package com.wrx.swing.form;

import com.wrx.dto.mndata.mnrawdata.MnRawDataVO;

import javax.swing.*;

/**
 * SQL 生成表单字段管理类
 * 统一管理表单中的所有输入字段
 */
public class MnRawDataFormFields {

    // 输入字段
    public final JTextField collectTimeStartField = new JTextField();
    public final JTextField collectTimeEndField = new JTextField();
    public final JTextField pointCodeNameField = new JTextField();
    public final JTextField tableNameField = new JTextField();
    public final JTextField tenantIdField = new JTextField();
    public final JTextField tenantNameField = new JTextField();
    public final JTextField dataBaseNameField = new JTextField();
    public final JTextField startIdField = new JTextField();
    public final JTextField filePathField = new JTextField();

    // 开始id开关
    public final JCheckBox useStartIdCheckBox = new JCheckBox("指定开始id");
    
    // 默认参数开关
    public final JCheckBox defaultParamsCheckBox = new JCheckBox("默认参数");
    
    // 模拟类型选择（单选框）
    public final ButtonGroup simulateTypeGroup = new ButtonGroup();
    public final JRadioButton generateSqlRadio = new JRadioButton("生成SQL");
    public final JRadioButton insertDbRadio = new JRadioButton("插入数据库");
    public final JRadioButton generateJsonRadio = new JRadioButton("生成JSON", true);
    public final JTextField userNameField = new JTextField();
    public final JPasswordField passwordField = new JPasswordField();
    public final JTextField databaseUrlField = new JTextField();

    // 返回SQL区域
    public final JTextArea resultSqlArea = new JTextArea();
    
    // 返回JSON区域
    public final JTextArea resultJsonArea = new JTextArea();
    
    // 返回变量code区域
    public final JTextArea resultPointCodeArea = new JTextArea();
    
    // 日志区域
    public final JTextArea logArea = new JTextArea();

    public MnRawDataFormFields() {
        // 文件路径字段设置为只读
        filePathField.setEditable(false);

        // 输入列宽
        collectTimeStartField.setColumns(20);
        collectTimeEndField.setColumns(20);
        pointCodeNameField.setColumns(20);
        tableNameField.setColumns(20);
        tenantIdField.setColumns(20);
        tenantNameField.setColumns(20);
        dataBaseNameField.setColumns(20);
        startIdField.setColumns(20);
        userNameField.setColumns(20);
        passwordField.setColumns(20);
        databaseUrlField.setColumns(20);
        filePathField.setColumns(25);
        
        // 返回SQL区域设置为只读
        resultSqlArea.setEditable(false);
        resultSqlArea.setLineWrap(true);
        resultSqlArea.setWrapStyleWord(true);
        
        // 返回JSON区域设置为只读
        resultJsonArea.setEditable(false);
        resultJsonArea.setLineWrap(true);
        resultJsonArea.setWrapStyleWord(true);
        
        // 返回变量code区域设置为只读
        resultPointCodeArea.setEditable(false);
        resultPointCodeArea.setLineWrap(true);
        resultPointCodeArea.setWrapStyleWord(true);

        // 默认值
        collectTimeStartField.setText("2025-11-01");
        collectTimeEndField.setText("2025-11-02");
        tableNameField.setText("zhsc_data_raw_data");
        tenantIdField.setText("132");
        dataBaseNameField.setText("zhsc_zhsw");
        useStartIdCheckBox.setSelected(false);
        defaultParamsCheckBox.setSelected(false);
        tenantNameField.setText("三水");
        // 初始化单选框组
        simulateTypeGroup.add(generateJsonRadio);
        simulateTypeGroup.add(generateSqlRadio);
        simulateTypeGroup.add(insertDbRadio);
        generateJsonRadio.setSelected(true); // 默认选择"生成JSON"
        userNameField.setText("root");
        passwordField.setText("Aotu123!");
        databaseUrlField.setText("192.168.0.132");
    }

    /**
     * 从表单字段构建 DTO 对象
     */
    public MnRawDataVO buildDto() {
        MnRawDataVO dto = new MnRawDataVO();
        dto.setCollectTimeStart(collectTimeStartField.getText());
        dto.setCollectTimeEnd(collectTimeEndField.getText());
        dto.setPointCodeNameList(pointCodeNameField.getText());
        dto.setTableName(tableNameField.getText());
        dto.setDatabaseName(dataBaseNameField.getText());
        dto.setTenantId(tenantIdField.getText());
        dto.setTenantName(tenantNameField.getText());
        // 根据单选框选择设置模拟类型
        String simulateType;
        if (insertDbRadio.isSelected()) {
            simulateType = "INSERT_DB";
        } else if (generateJsonRadio.isSelected()) {
            simulateType = "GENERATE_JSON";
        } else {
            simulateType = "GENERATE_SQL"; // 默认生成SQL
        }
        dto.setSimulateType(simulateType);
        
        // 如果是插入数据库或生成SQL模式，可以设置开始id（如果填写了的话）
        if ("INSERT_DB".equals(simulateType) || "GENERATE_SQL".equals(simulateType)) {
            dto.setStartId(parseInteger(startIdField.getText()));
        } else {
            dto.setStartId(null);
        }
        
        // 如果是插入数据库模式，需要设置数据库连接信息
        if ("INSERT_DB".equals(simulateType)) {
            dto.setUserName(userNameField.getText());
            dto.setPassword(new String(passwordField.getPassword()));
            dto.setDatabaseUrl(databaseUrlField.getText());
        } else {
            dto.setUserName(null);
            dto.setPassword(null);
            dto.setDatabaseUrl(null);
        }
        return dto;
    }

    private Integer parseInteger(String value) {
        String text = value == null ? "" : value.trim();
        if (text.isEmpty()) {
            return null;
        }
        return Integer.valueOf(text);
    }
}

